package cn.com.byte1024.service.impl;

import cn.com.byte1024.entity.*;
import cn.com.byte1024.mapper.*;
import cn.com.byte1024.service.VoteProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: VoteSystem
 * @ClassName: VoteProjectServiceImpl
 * @Description:
 * @Author: Kwnda
 * @Date: 2021/1/5 15:06
 */
@Service
public class VoteProjectServiceImpl implements VoteProjectService {
    
    @Autowired
    private VoteProjectMapper voteProjectMapper;
    
    @Autowired
    private UserVoteProjectMapper userVoteProjectMapper;
    
    @Autowired
    private VoteMapper voteMapper;
    
    @Autowired
    private VoteLogMapper voteLogMapper;

    @Autowired
    private VoteProjectVoteMapper voteProjectVoteMapper;


    @Override
    public int updateByPrimaryKeySelective(VoteProject voteProject) {
        return voteProjectMapper.updateByPrimaryKeySelective(voteProject);
    }

    @Override
    public VoteProject selectByPrimaryKey(String voteProjectId) {
        return voteProjectMapper.selectByPrimaryKey(voteProjectId);
    }

    @Override
    public int insertSelective(VoteProject voteProject) {
        int num = voteProjectMapper.insertSelective(voteProject);
        UserVoteProjectKey userVoteProjectKey = new UserVoteProjectKey();
        userVoteProjectKey.setVoteProjectId(voteProject.getVoteProjectId());
        userVoteProjectKey.setUserId(voteProject.getUserId());
        userVoteProjectMapper.insertSelective(userVoteProjectKey);
        return num;
    }

    @Override
    public List<VoteProject> selectAllVoteProject(String userId) {
        //先通过userId在表user_vote_project查询项目id，再通过项目id查询表vote_project的项目
        List<UserVoteProjectKey> userVoteProjectList = userVoteProjectMapper.selectUserVoteProject(userId);
        List<VoteProject> voteProjectList= new ArrayList<>();
        for (UserVoteProjectKey userVoteProjectKey:userVoteProjectList) {
            VoteProject voteProject=voteProjectMapper.selectByPrimaryKey(userVoteProjectKey.getVoteProjectId());
            voteProjectList.add(voteProject);
        }
        return voteProjectList;
    }

    @Override
    public VoteProject selectByProjectCode(String projectCode) {
        return voteProjectMapper.selectByProjectCode(projectCode);
    }

    @Override
    public Boolean addVoteProjectByProjectCode(String projectCode, String userId) {
        boolean flag;
        VoteProject voteProject =selectByProjectCode(projectCode);
        if(voteProject!=null){
            UserVoteProjectKey userVoteProjectKey = new UserVoteProjectKey();
            userVoteProjectKey.setUserId(userId);
            userVoteProjectKey.setVoteProjectId(  voteProject.getVoteProjectId());
            userVoteProjectMapper.insert(userVoteProjectKey);
            flag=true;
        }else{
            flag=false;
        }
        return flag;
    }

    @Override
    public Boolean deleteByVoteProject(String voteProjectId, String userId) {
        boolean flag;
        //通过项目id和用户id查询，该删除的项目是否发起者的项目
        VoteProject voteProject =voteProjectMapper.selectByVoteProjectIdUserId(voteProjectId,userId);

        if(voteProject!=null){
            //是 删除项目
            UserVoteProjectKey userVoteProjectKey = new UserVoteProjectKey();
            userVoteProjectKey.setUserId(userId);
            userVoteProjectKey.setVoteProjectId(voteProjectId);
            userVoteProjectMapper.deleteByPrimaryKey(userVoteProjectKey);
            
            voteProjectMapper.deleteByPrimaryKey(voteProjectId);
            //先查询出该项目下所有的投票
            List<VoteProjectVoteKey> voteProjectVoteList= voteProjectVoteMapper.selectAllVoteProjectVoteByVoteProjectId(voteProjectId);
            for (VoteProjectVoteKey voteProjectVoteKey:voteProjectVoteList) {
                //删除 投票
                voteMapper.deleteByPrimaryKey(voteProjectVoteKey.getVoteId());
                //删除 投票记录
                voteLogMapper.deleteByVoteId(voteProjectVoteKey.getVoteId());
            }
            //删除投票项目和投票表对应记录
            voteProjectVoteMapper.deleteByVoteProjectId(voteProjectId);
            flag=true;
        }else{
            //否，则不删除
            flag=false;
        }
        return flag;
    }
}
